home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xtetris-2.5.2 / shape.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-09  |  8.4 KB  |  228 lines

  1. #include "defs.h"
  2. #ifdef SVR4
  3. # define random rand
  4. #endif
  5.  
  6. shape_type shape[7] = {
  7.  
  8. /*      bitmap    score X Y wid ht */
  9.   { /* Shape 0 */
  10.     { { 0x00000f00, 5,  { {0,1,4,1}, {0,0,0,0} }, {{0,0,0,0},{0,0,0,0}}, 1,  0,4,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*      */
  11.       { 0x00004444, 8,  { {1,0,1,4}, {0,0,0,0} }, {{0,0,0,0},{0,0,0,0}}, 1,  1,1,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /* #### */
  12.       { 0x00000f00, 5,  { {0,1,4,1}, {0,0,0,0} }, {{0,0,0,0},{0,0,0,0}}, 1,  0,4,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*      */
  13.       { 0x00004444, 8,  { {1,0,1,4}, {0,0,0,0} }, {{0,0,0,0},{0,0,0,0}}, 1,  1,1,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, /*      */
  14.   },
  15.  
  16.   { /* Shape 1 */
  17.     { { 0x0000cc00, 6,  { {0,0,2,2}, {0,0,0,0} }, {{0,0,0,0},{0,0,0,0}}, 1,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /* ##   */
  18.       { 0x0000cc00, 6,  { {0,0,2,2}, {0,0,0,0} }, {{0,0,0,0},{0,0,0,0}}, 1,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /* ##   */
  19.       { 0x0000cc00, 6,  { {0,0,2,2}, {0,0,0,0} }, {{0,0,0,0},{0,0,0,0}}, 1,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*      */
  20.       { 0x0000cc00, 6,  { {0,0,2,2}, {0,0,0,0} }, {{0,0,0,0},{0,0,0,0}}, 1,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, /*      */
  21.   },
  22.  
  23.   { /* Shape 2 */
  24.     { { 0x00004e00, 5,  { {0,1,3,1}, {1,0,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*  #   */
  25.       { 0x00004640, 5,  { {1,0,1,3}, {2,1,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  1,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /* ###  */
  26.       { 0x00000e40, 6,  { {0,1,3,1}, {1,2,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*      */
  27.       { 0x00004c40, 5,  { {1,0,1,3}, {0,1,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, /*      */
  28.   },
  29.  
  30.   { /* Shape 3 */
  31.     { { 0x0000c600, 6,  { {0,0,2,1}, {1,1,2,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /* ##   */
  32.       { 0x00004c80, 7,  { {1,0,1,2}, {0,1,1,2} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*  ##  */
  33.       { 0x0000c600, 6,  { {0,0,2,1}, {1,1,2,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*      */
  34.       { 0x00004c80, 7,  { {1,0,1,2}, {0,1,1,2} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, /*      */
  35.   },
  36.  
  37.   { /* Shape 4 */
  38.     { { 0x00006c00, 6,  { {1,0,2,1}, {0,1,2,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*  ##  */
  39.       { 0x00008c40, 7,  { {0,0,1,2}, {1,1,1,2} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /* ##   */
  40.       { 0x00006c00, 6,  { {1,0,2,1}, {0,1,2,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*      */
  41.       { 0x00008c40, 7,  { {0,0,1,2}, {1,1,1,2} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, /*      */
  42.   },
  43.  
  44.   { /* Shape 5 */
  45.     { { 0x00002e00, 6,  { {2,0,1,1}, {0,1,3,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*   #  */
  46.       { 0x000088c0, 7,  { {0,0,1,3}, {1,2,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /* ###  */
  47.       { 0x0000e800, 6,  { {0,0,3,1}, {0,1,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*      */
  48.       { 0x0000c440, 7,  { {1,0,1,3}, {0,0,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, /*      */
  49.   },
  50.  
  51.   { /* Shape 6 */
  52.     { { 0x0000e200, 6,  { {0,0,3,1}, {2,1,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /* ###  */
  53.       { 0x000044c0, 7,  { {1,0,1,3}, {0,2,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*   #  */
  54.       { 0x00008e00, 6,  { {0,1,3,1}, {0,0,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,3,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} },   /*      */
  55.       { 0x0000c880, 7,  { {0,0,1,3}, {1,0,1,1} }, {{0,0,0,0},{0,0,0,0}}, 2,  0,2,  {0,0,0,0}, {0,0,0,0}, {0,0,0,0} } }, /*      */
  56.   }
  57. };
  58.  
  59. void define_shapes()
  60. {
  61.   Pixmap boxstipple;
  62.   int s, r, x, y;
  63.   
  64.   /* First create the stipple for the boxes, depending on the box size. */
  65.   {
  66.     XGCValues gcv;
  67.     GC cleargc, setgc, checkgc;
  68.     Pixmap graytile;
  69.     static XPoint points[] = 
  70.     { 
  71.       { 0,1 }, 
  72.       { 1,0 }, 
  73.     };
  74.  
  75.     /* This long, complicated thing is intended to reduce traffic to the server: 
  76.        create a gray stipple first, then spray it over the box to make the final
  77.        stipple. */
  78.  
  79.     graytile = XCreatePixmap( XtDisplay(canvas), XtWindow(canvas), 2, 2, 1 );
  80.     gcv.foreground = gcv.background = 0;
  81.     cleargc = XCreateGC( XtDisplay(canvas), graytile, (unsigned long) GCForeground|GCBackground, &gcv );
  82.     gcv.foreground = 1; 
  83.     setgc = XCreateGC( XtDisplay(canvas), graytile, (unsigned long) GCForeground|GCBackground, &gcv );
  84.     XFillRectangle( XtDisplay(canvas), graytile, cleargc, 0, 0, 2, 2 );
  85.     XDrawPoints( XtDisplay(canvas), graytile, setgc, points, 2, CoordModeOrigin );
  86.  
  87.     /* Now create the box. */
  88.  
  89.     boxstipple = XCreatePixmap( XtDisplay(canvas), XtWindow(canvas), 
  90.                    resources.boxsize, resources.boxsize, 1 );
  91.     gcv.tile = graytile;
  92.     gcv.fill_style = FillTiled;
  93.     checkgc = XCreateGC( XtDisplay(canvas), boxstipple, 
  94.             (unsigned long) GCForeground|GCBackground|GCTile|GCFillStyle, &gcv );
  95.     XFillRectangle( XtDisplay(canvas), boxstipple, checkgc, 0, 0, 
  96.            resources.boxsize-1, resources.boxsize-1 );
  97.     XDrawLine( XtDisplay(canvas), boxstipple, setgc, 0, 0, resources.boxsize-2, 0 );
  98.     XDrawLine( XtDisplay(canvas), boxstipple, setgc, 0, 1, 0, resources.boxsize-2 );
  99.     XDrawLine( XtDisplay(canvas), boxstipple, cleargc, resources.boxsize-1, 0, 
  100.           resources.boxsize-1, resources.boxsize-1 );
  101.     XDrawLine( XtDisplay(canvas), boxstipple, cleargc, 0, resources.boxsize-1,
  102.           resources.boxsize-2, resources.boxsize-1 );
  103.     
  104.     /* Now free up all this garbage. */
  105.  
  106.     XFreeGC( XtDisplay(canvas), setgc );
  107.     XFreeGC( XtDisplay(canvas), cleargc );
  108.     XFreeGC( XtDisplay(canvas), checkgc );
  109.     XFreePixmap( XtDisplay(canvas), graytile );
  110.   }
  111.  
  112.   /* First set the highesty values (could have specified these statically, but the
  113.      human cost is too high.  This is one-shot anyway). */
  114.  
  115.   for (s = 0; s < 7; s++)
  116.     {
  117.       shape_ptr sh = &shape[s];
  118.       XGCValues gcv;
  119.  
  120.       for (r = 0; r < 4; r++)
  121.     {
  122.       /* working on shape s */
  123.  
  124.       rotshape_ptr rot = &sh->forms[r];
  125.       unsigned long unitson;
  126.       int i;
  127.  
  128.  
  129.       /* Scale the x,y,width,length */
  130.  
  131.       for (i = 0; i < rot->nrect; i++)
  132.         {
  133.           XRectangle *rec = &rot->rect[i];
  134.  
  135.           *rec = rot->urect[i];
  136.  
  137.           rec->x *= resources.boxsize;
  138.           rec->y *= resources.boxsize;
  139.           rec->width *= resources.boxsize;
  140.           rec->height *= resources.boxsize;
  141.         }
  142.  
  143.       rot->shadowx *= resources.boxsize;
  144.       rot->shadowwidth *= resources.boxsize;
  145.  
  146.       /* set the highesty values. */
  147.  
  148.       unitson = rot->unitson;
  149.       for (x = 0; x < 4; x++)
  150.         {
  151.           rot->highesty[x] = rot->highestx[x] = 0;
  152.           rot->lowestx[x] = -2;
  153.  
  154.         }
  155.  
  156.       for (y = 3; y >= 0; y -- )
  157.         for (x = 3; x >= 0; x--)
  158.           {
  159.         if (unitson & 1)
  160.           {
  161.             if (rot->highesty[x] == 0)
  162.               rot->highesty[x] = y+1;
  163.             if (rot->highestx[y] == 0)
  164.               rot->highestx[y] = x+1;
  165.             rot->lowestx[y] = x-1;
  166.           }
  167.         unitson >>= 1;
  168.           }
  169.     }
  170.  
  171.       /* Now allocate the graphics context, and set it properly. */
  172.  
  173.       gcv.foreground = sh->foreground;
  174.       gcv.background = sh->background;
  175.       gcv.stipple = boxstipple;
  176.       gcv.fill_style = FillOpaqueStippled;
  177.  
  178.       sh->gc = XCreateGC( XtDisplay(canvas), XtWindow(canvas),
  179.              (unsigned long) GCForeground|GCBackground|GCStipple|GCFillStyle,
  180.              &gcv );
  181.     }
  182. }
  183.  
  184.  
  185. void store_shape(shape_no, xpos, ypos, rot)
  186.   int     shape_no, xpos, ypos, rot;
  187. {
  188.   register unsigned long unitson = shape[shape_no].forms[rot].unitson;
  189.  
  190.   int y, x;
  191.  
  192.   for (y = ypos+3; y >= ypos; y--)
  193.     for (x = xpos+3; x >= xpos; x--)
  194.       {
  195.  
  196.     if ((x >= 0) && (y >= 0))
  197.       if (unitson & 0x00000001)
  198.         grid[x][y] = &shape[shape_no];
  199.     unitson >>= 1;
  200.       }
  201. }
  202.  
  203. extern long random();
  204.  
  205. void create_shape()
  206. {
  207.   shape_no = next_no;
  208.   rot = next_rot;
  209.   next_no = random() % 7;
  210.   next_rot = random() % 4;
  211.   xpos = (UWIDTH / 2) - 1;
  212.   ypos = -4;
  213. }
  214. /*
  215. emacs mode: indented-text
  216.  
  217. emacs Local Variables: 
  218. emacs mode: c 
  219. emacs c-indent-level: 2
  220. emacs c-continued-statement-offset: 2
  221. emacs c-continued-brace-offset: -2
  222. emacs c-tab-always-indent: nil
  223. emacs c-brace-offset: 0 
  224. emacs tab-width: 8
  225. emacs tab-stop-list: (2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84)
  226. emacs End:
  227. */
  228.